<?php
/**
 * Created by PhpStorm.
 * User: fyl
 * Date: 2016/11/23
 * Time: 14:14
 */

/**
 * Class Involved_DealRecordModel
 */

class Involved_DealRecordModel extends BaseModel
{

    protected $table = 'deal_record';


    /**
     * 资金流水充值默认查询
     * @param $user_id
     * @return mixed
     */

    public function UserAccountsCapitalflow($user_id,$page,$page_size)
    {
        if($page == 1){
            $page = $page - 1;
        }else{
            $page =$page_size*($page-1);
        }
        $db =   DB::table($this->table)
            ->select('event_id','type','deal_record.user_id','deal_record.amount','deal_record.add_time','update_after')
            ->where('deal_record.user_id',$user_id)
            ->orderBy('deal_record.add_time', 'desc')
            ->skip($page)->take($page_size)
            ->get();
        foreach($db as $k=>$v){
            if($v->type=='DE_CASH'){
               $db2 = DB::table('deal_cash')->select('status')->where('auto_id',$v->event_id)->first();
               $v->status = $db2->status;
            }
        }
        return $db;
    }

    /**
     * 资金流水条数充值默认查询
     * @param $user_id
     * @return mixed
     */
    public function CapitalCount($user_id)
    {
        return  DB::table($this->table)
            ->select('auto_id','type')
            ->where('deal_record.user_id',$user_id)
//            ->where('deal_record.type','IN_DEPOSIT')
            ->get();
    }


    /**
     * 资金流水精确条数查询
     * @param $user_id
     * @param $paramObj
     * @return mixed
     */
    public function CapitalExactCount($user_id,$paramObj)
    {


        $db =  DB::table($this->table);
        $begin_time = strtotime($paramObj->begin_time);
        if($paramObj->type_val=='deposit'){
            $type_val = 'IN_DEPOSIT';
            $db ->where('deal_record.type',$type_val);
        }elseif($paramObj->type_val=='cash'){
            $type_val = 'DE_CASH';
            $db ->where('deal_record.type',$type_val);
        }elseif($paramObj->type_val=='invest'){
            $type_val = 'DE_INVEST';
            $db ->where('deal_record.type',$type_val);
        }elseif($paramObj->type_val=='refund'){
            $type_val = 'IN_REFUND';
            $db ->where('deal_record.type',$type_val);
        }else{

        }

        if(!empty($paramObj->begin_time)&&!empty($paramObj->end_time))
        {
            $end_time = strtotime($paramObj->end_time)+86400;
            $data = $db
                ->select('auto_id')
                ->where('deal_record.user_id',$user_id)
//                ->where('deal_record.type',$type_val)
                ->whereBetween('deal_record.add_time',array($begin_time,$end_time))
                ->get();
            return $data;
        }
        if(!empty($paramObj->begin_time))
        {
            $data = $db
                ->select('auto_id')
                ->where('deal_record.user_id',$user_id)
//                ->where('deal_record.type',$type_val)
                ->where('deal_record.add_time','>',$begin_time)
                ->get();
            return $data;
        }

        if(!empty($paramObj->end_time))
        {
            $end_time = strtotime($paramObj->end_time)+86400;
            $data = $db
                ->select('auto_id')
                ->where('deal_record.user_id',$user_id)
//                ->where('deal_record.type',$type_val)
                ->where('deal_record.add_time','<',$end_time)
                ->get();
            return $data;
        }

    }

    /**
     * 资金流水精确查询
     * @param $user_id
     * @param $paramObj
     * @param $page
     * @param $page_size
     * @return mixed
     */
    public function CapitalExactSel($user_id,$paramObj,$page,$page_size)
    {

        if($page == 1){
            $page = $page - 1;
        }else{
            $page =$page_size*($page-1);
        }
        $begin_time = strtotime($paramObj->begin_time);
        if(!empty($paramObj->begin_time)&&!empty($paramObj->end_time))
        {
            $end_time = strtotime($paramObj->end_time)+86400;
            if($paramObj->type_val=='deposit'){
                $type_val = 'IN_DEPOSIT';
                return  DB::table($this->table)
                    ->select('type','deal_record.amount','deal_record.add_time','update_after')
                    ->where('deal_record.user_id',$user_id)
                    ->where('deal_record.type',$type_val)
                    ->whereBetween('deal_record.add_time',array($begin_time,$end_time))
                    ->orderBy('deal_record.add_time', 'desc')
                    ->skip($page)->take($page_size)
                    ->get();
            }elseif($paramObj->type_val=='cash'){
                $type_val = 'DE_CASH';
                return  DB::table($this->table)
                    ->select('type','deal_record.amount','deal_record.add_time','deal_deposit.status','update_after')
                    ->LeftJoin('deal_deposit','deal_deposit.auto_id','=','deal_record.event_id')
                    ->where('deal_record.user_id',$user_id)
                    ->where('deal_record.type',$type_val)
                    ->whereBetween('deal_record.add_time',array($begin_time,$end_time))
                    ->orderBy('deal_record.add_time', 'desc')
                    ->skip($page)->take($page_size)
                    ->get();
            }elseif($paramObj->type_val=='invest'){
                $type_val = 'DE_INVEST';
                return  DB::table($this->table)
                    ->select('type','deal_record.amount','deal_record.add_time','update_after')
                    ->where('deal_record.user_id',$user_id)
                    ->where('deal_record.type',$type_val)
                    ->whereBetween('deal_record.add_time',array($begin_time,$end_time))
                    ->orderBy('deal_record.add_time', 'desc')
                    ->skip($page)->take($page_size)
                    ->get();
            }elseif($paramObj->type_val=='refund'){
                $type_val = 'IN_REFUND';
                return  DB::table($this->table)
                    ->select('type','deal_record.amount','deal_record.add_time','update_after')
                    ->where('deal_record.user_id',$user_id)
                    ->where('deal_record.type',$type_val)
                    ->whereBetween('deal_record.add_time',array($begin_time,$end_time))
                    ->orderBy('deal_record.add_time', 'desc')
                    ->skip($page)->take($page_size)
                    ->get();
            }else{
                $db =   DB::table($this->table)
                    ->select('event_id','type','deal_record.user_id','deal_record.amount','deal_record.add_time','update_after')
                    ->where('deal_record.user_id',$user_id)
                    ->whereBetween('deal_record.add_time',array($begin_time,$end_time))
                    ->orderBy('deal_record.add_time', 'desc')
                    ->skip($page)->take($page_size)
                    ->get();
                foreach($db as $k=>$v){
                    if($v->type=='DE_CASH'){
                        $db2 = DB::table('deal_cash')->select('status')->where('auto_id',$v->event_id)->first();
                        $v->status = $db2->status;
                    }
                }
                return $db;
            }
        }
        if(!empty($paramObj->begin_time))
        {
            if($paramObj->type_val=='deposit'){
                $type_val = 'IN_DEPOSIT';
                return  DB::table($this->table)
                    ->select('type','deal_record.amount','deal_record.add_time','deal_deposit.status','update_after')
                    ->LeftJoin('deal_deposit','deal_deposit.auto_id','=','deal_record.event_id')
                    ->where('deal_record.user_id',$user_id)
                    ->where('deal_record.type',$type_val)
                    ->where('deal_record.add_time','>',$begin_time)
                    ->orderBy('deal_record.add_time', 'desc')
                    ->skip($page)->take($page_size)
                    ->get();
            }elseif($paramObj->type_val=='cash'){
                $type_val = 'DE_CASH';
                return  DB::table($this->table)
                    ->select('type','deal_record.amount','deal_record.add_time','deal_deposit.status','update_after')
                    ->LeftJoin('deal_deposit','deal_deposit.auto_id','=','deal_record.event_id')
                    ->where('deal_record.user_id',$user_id)
                    ->where('deal_record.type',$type_val)
                    ->where('deal_record.add_time','>',$begin_time)
                    ->orderBy('deal_record.add_time', 'desc')
                    ->skip($page)->take($page_size)
                    ->get();
            }elseif($paramObj->type_val=='invest'){
                $type_val = 'DE_INVEST';
                return  DB::table($this->table)
                    ->select('type','deal_record.amount','deal_record.add_time','deal_deposit.status','update_after')
                    ->LeftJoin('deal_deposit','deal_deposit.auto_id','=','deal_record.event_id')
                    ->where('deal_record.user_id',$user_id)
                    ->where('deal_record.type',$type_val)
                    ->where('deal_record.add_time','>',$begin_time)
                    ->orderBy('deal_record.add_time', 'desc')
                    ->skip($page)->take($page_size)
                    ->get();
            }elseif($paramObj->type_val=='refund') {
                $type_val = 'IN_REFUND';
                return  DB::table($this->table)
                    ->select('type','deal_record.amount','deal_record.add_time','deal_deposit.status','update_after')
                    ->LeftJoin('deal_deposit','deal_deposit.auto_id','=','deal_record.event_id')
                    ->where('deal_record.user_id',$user_id)
                    ->where('deal_record.type',$type_val)
                    ->where('deal_record.add_time','>',$begin_time)
                    ->orderBy('deal_record.add_time', 'desc')
                    ->skip($page)->take($page_size)
                    ->get();
            }else{
                $db =   DB::table($this->table)
                    ->select('event_id','type','deal_record.user_id','deal_record.amount','deal_record.add_time','update_after')
                    ->where('deal_record.user_id',$user_id)
                    ->where('deal_record.add_time','>',$begin_time)
                    ->orderBy('deal_record.add_time', 'desc')
                    ->skip($page)->take($page_size)
                    ->get();
                foreach($db as $k=>$v){
                    if($v->type=='DE_CASH'){
                        $db2 = DB::table('deal_cash')->select('status')->where('auto_id',$v->event_id)->first();
                        $v->status = $db2->status;
                    }
                }
                return $db;
            }

        }

        if(!empty($paramObj->end_time))
        {

            $end_time = strtotime($paramObj->end_time)+86400;
            if($paramObj->type_val=='deposit'){
                $type_val = 'IN_DEPOSIT';
                return  DB::table($this->table)
                    ->select('type','deal_record.amount','deal_record.add_time','deal_deposit.status','update_after')
                    ->LeftJoin('deal_deposit','deal_deposit.auto_id','=','deal_record.event_id')
                    ->where('deal_record.user_id',$user_id)
                    ->where('deal_record.type',$type_val)
                    ->where('deal_record.add_time','<',$end_time)
                    ->orderBy('deal_record.add_time', 'desc')
                    ->skip($page)->take($page_size)
                    ->get();
            }elseif($paramObj->type_val=='cash'){
                $type_val = 'DE_CASH';
                return  DB::table($this->table)
                    ->select('type','deal_record.amount','deal_record.add_time','deal_deposit.status','update_after')
                    ->LeftJoin('deal_deposit','deal_deposit.auto_id','=','deal_record.event_id')
                    ->where('deal_record.user_id',$user_id)
                    ->where('deal_record.type',$type_val)
                    ->where('deal_record.add_time','<',$end_time)
                    ->orderBy('deal_record.add_time', 'desc')
                    ->skip($page)->take($page_size)
                    ->get();
            }elseif($paramObj->type_val=='invest'){
                $type_val = 'DE_INVEST';
                return  DB::table($this->table)
                    ->select('type','deal_record.amount','deal_record.add_time','deal_deposit.status','update_after')
                    ->LeftJoin('deal_deposit','deal_deposit.auto_id','=','deal_record.event_id')
                    ->where('deal_record.user_id',$user_id)
                    ->where('deal_record.type',$type_val)
                    ->where('deal_record.add_time','<',$end_time)
                    ->orderBy('deal_record.add_time', 'desc')
                    ->skip($page)->take($page_size)
                    ->get();
            }elseif($paramObj->type_val=='refund'){
                $type_val = 'IN_REFUND';
                return  DB::table($this->table)
                    ->select('type','deal_record.amount','deal_record.add_time','deal_deposit.status','update_after')
                    ->LeftJoin('deal_deposit','deal_deposit.auto_id','=','deal_record.event_id')
                    ->where('deal_record.user_id',$user_id)
                    ->where('deal_record.type',$type_val)
                    ->where('deal_record.add_time','<',$end_time)
                    ->orderBy('deal_record.add_time', 'desc')
                    ->skip($page)->take($page_size)
                    ->get();
            }else{
                $db =   DB::table($this->table)
                    ->select('event_id','type','deal_record.user_id','deal_record.amount','deal_record.add_time','update_after')
                    ->where('deal_record.user_id',$user_id)
                    ->where('deal_record.add_time','<',$end_time)
                    ->orderBy('deal_record.add_time', 'desc')
                    ->skip($page)->take($page_size)
                    ->get();
                foreach($db as $k=>$v){
                    if($v->type=='DE_CASH'){
                        $db2 = DB::table('deal_cash')->select('status')->where('auto_id',$v->event_id)->first();
                        $v->status = $db2->status;
                    }
                }
                return $db;
            }
        }

    }


    /**
     * 资金流水范围条数查询
     * @param $user_id
     * @param $paramObj
     * @return mixed
     */
    public function CapitalRangeCount($user_id,$paramObj)
    {


        if($paramObj->time_val=='week'){
            $time_val = time()-86400*7;
        }elseif($paramObj->time_val=='month'){
            $time_val = time()-86400*30;
        }else{
            $time_val = '';
        }

        if($paramObj->type_val=='deposit'){
            $type_val = 'IN_DEPOSIT';
        }elseif($paramObj->type_val=='cash'){
            $type_val = 'DE_CASH';
        }elseif($paramObj->type_val=='invest'){
            $type_val = 'DE_INVEST';
        }elseif($paramObj->type_val=='refund'){
            $type_val = 'IN_REFUND';
        }

        if(!empty($time_val)){
            if($paramObj->type_val=='all'){
                return DB::table($this->table)
                    ->select('auto_id')
                    ->where('deal_record.user_id',$user_id)
                    ->whereBetween('deal_record.add_time',array($time_val,time()))
                    ->get();
            }else{
                return DB::table($this->table)
                    ->select('auto_id')
                    ->where('deal_record.user_id',$user_id)
                    ->where('deal_record.type',$type_val)
                    ->whereBetween('deal_record.add_time',array($time_val,time()))
                    ->get();
            }
        }else{
            if($paramObj->type_val=='all'){
                return DB::table($this->table)
                    ->select('auto_id')
                    ->where('deal_record.user_id',$user_id)
                    ->get();
            }else{
                return DB::table($this->table)
                    ->select('auto_id')
                    ->where('deal_record.user_id',$user_id)
                    ->where('deal_record.type',$type_val)
                    ->get();
            }

        }


    }


    /**
     * 资金流水范围查询
     * @param $user_id
     * @param $paramObj
     * @param $page
     * @param $page_size
     * @return mixed
     */
    public function CapitalRangeSel($user_id,$paramObj,$page,$page_size)
    {

        if($page == 1){
            $page = $page - 1;
        }else{
            $page =$page_size*($page-1);
        }
        if($paramObj->time_val=='week'){
            $time_val = time()-86400*7;
        }elseif($paramObj->time_val=='month'){
            $time_val = time()-86400*30;
        }else{
            $time_val = '';
        }
        if($paramObj->type_val=='deposit'){
            $type_val = 'IN_DEPOSIT';
            if(!empty($time_val)){
                return  DB::table($this->table)
                    ->select('type','deal_record.amount','deal_record.add_time','update_after')
                    ->where('deal_record.user_id',$user_id)
                    ->where('deal_record.type',$type_val)
                    ->whereBetween('deal_record.add_time',array($time_val,time()))
                    ->orderBy('deal_record.add_time', 'desc')
                    ->skip($page)->take($page_size)
                    ->get();
            }else{
                return  DB::table($this->table)
                    ->select('type','deal_record.amount','deal_record.add_time','update_after')
                    ->where('deal_record.user_id',$user_id)
                    ->where('deal_record.type',$type_val)
                    ->orderBy('deal_record.add_time', 'desc')
                    ->skip($page)->take($page_size)
                    ->get();
            }
        }elseif($paramObj->type_val=='cash'){
            $type_val = 'DE_CASH';
            if(!empty($time_val)){
                return  DB::table($this->table)
                    ->select('type','deal_record.amount','deal_record.add_time','deal_cash.status','update_after')
                    ->LeftJoin('deal_cash','deal_cash.auto_id','=','deal_record.event_id')
                    ->where('deal_record.user_id',$user_id)
                    ->where('deal_record.type',$type_val)
                    ->whereBetween('deal_record.add_time',array($time_val,time()))
                    ->orderBy('deal_record.add_time', 'desc')
                    ->skip($page)->take($page_size)
                    ->get();
            }else{
                return  DB::table($this->table)
                    ->select('type','deal_record.amount','deal_record.add_time','deal_cash.status','update_after')
                    ->LeftJoin('deal_cash','deal_cash.auto_id','=','deal_record.event_id')
                    ->where('deal_record.user_id',$user_id)
                    ->where('deal_record.type',$type_val)
                    ->orderBy('deal_record.add_time', 'desc')
                    ->skip($page)->take($page_size)
                    ->get();
            }
        }elseif($paramObj->type_val=='invest'){
            $type_val = 'DE_INVEST';
            if(!empty($time_val)){
                return  DB::table($this->table)
                    ->select('type','deal_record.amount','deal_record.add_time','update_after')
                    ->where('deal_record.user_id',$user_id)
                    ->where('deal_record.type',$type_val)
                    ->whereBetween('deal_record.add_time',array($time_val,time()))
                    ->orderBy('deal_record.add_time', 'desc')
                    ->skip($page)->take($page_size)
                    ->get();
            }else{
                return  DB::table($this->table)
                    ->select('type','deal_record.amount','deal_record.add_time','update_after')
                    ->where('deal_record.user_id',$user_id)
                    ->where('deal_record.type',$type_val)
                    ->orderBy('deal_record.add_time', 'desc')
                    ->skip($page)->take($page_size)
                    ->get();
            }
        }elseif($paramObj->type_val=='refund'){
            $type_val = 'IN_REFUND';
            if(!empty($time_val)){
                return  DB::table($this->table)
                    ->select('type','deal_record.amount','deal_record.add_time','update_after')
                    ->where('deal_record.user_id',$user_id)
                    ->where('deal_record.type',$type_val)
                    ->whereBetween('deal_record.add_time',array($time_val,time()))
                    ->orderBy('deal_record.add_time', 'desc')
                    ->skip($page)->take($page_size)
                    ->get();
            }else{
                return  DB::table($this->table)
                    ->select('type','deal_record.amount','deal_record.add_time','update_after')
                    ->where('deal_record.user_id',$user_id)
                    ->where('deal_record.type',$type_val)
                    ->orderBy('deal_record.add_time', 'desc')
                    ->skip($page)->take($page_size)
                    ->get();
            }
        }else{
            if(!empty($time_val)){
                $db =   DB::table($this->table)
                    ->select('event_id','type','deal_record.user_id','deal_record.amount','deal_record.add_time','update_after')
                    ->where('deal_record.user_id',$user_id)
                    ->whereBetween('deal_record.add_time',array($time_val,time()))
                    ->orderBy('deal_record.add_time', 'desc')
                    ->skip($page)->take($page_size)
                    ->get();
                foreach($db as $k=>$v){
                    if($v->type=='DE_CASH'){
                        $db2 = DB::table('deal_cash')->select('status')->where('auto_id',$v->event_id)->first();
                        $v->status = $db2->status;
                    }
                }
                return $db;
            }else {
                $db = DB::table($this->table)
                    ->select('event_id', 'type', 'deal_record.user_id', 'deal_record.amount', 'deal_record.add_time', 'update_after')
                    ->where('deal_record.user_id', $user_id)
                    ->orderBy('deal_record.add_time', 'desc')
                    ->skip($page)->take($page_size)
                    ->get();
                foreach ($db as $k => $v) {
                    if ($v->type == 'DE_CASH') {
                        $db2 = DB::table('deal_cash')->select('status')->where('auto_id', $v->event_id)->first();
                        $v->status = $db2->status;
                    }
                }
                return $db;
            }
        }

    }


    /**
     * Web资金流水
     * @param $user_id
     * @param $type
     * @return mixed
     */
    public function WebMoneyRecord($user_id,$type,$page)
    {
        $offset = ($page-1) * HostEnum::PAGE_NUM;
        if(empty($type)){
            $db =   DB::table('deal_record')
                ->select('event_id','type','deal_record.user_id','deal_record.amount','deal_record.add_time','update_after')
                ->where('deal_record.user_id',$user_id)
                ->orderBy('deal_record.add_time', 'desc')
                ->skip($offset)->take(HostEnum::PAGE_NUM)
                ->get();
            foreach($db as $k=>$v){
                if($v->type=='DE_CASH'){
                    $db2 = DB::table('deal_cash')->select('status')->where('auto_id',$v->event_id)->first();
                    $v->status = $db2->status;
                }
            }
            return $db;
        }elseif($status='DE_CASH'){
            return  DB::table('deal_record')
                ->select('type','deal_record.amount','deal_record.add_time','deal_deposit.status','update_after')
                ->LeftJoin('deal_deposit','deal_deposit.auto_id','=','deal_record.event_id')
                ->where('deal_record.user_id',$user_id)
                ->where('deal_record.type',$type)
                ->orderBy('deal_record.add_time', 'desc')
                ->skip($offset)->take(HostEnum::PAGE_NUM)
                ->get();
        }else{
            return  DB::table('deal_record')
                ->select('type','deal_record.amount','deal_record.add_time','update_after')
                ->LeftJoin('item','item.auto_id','=','invest.item_id')
                ->where('deal_record.user_id',$user_id)
                ->where('deal_record.type',$type)
                ->orderBy('invest.add_time', 'desc')
                ->skip($offset)->take(HostEnum::PAGE_NUM)
                ->get();
        }
    }


}